package View;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.freixas.jcalendar.JCalendarCombo;

import Events.Alarm;
import Events.Appointment;
import Events.GenericEvent;
import Events.Reminder;
import Events.Task;


public class AddFrame implements ActionListener 
{
	
	private JFrame frame ;
	private String txt="";
	private JLabel panel_header, panel_config, panel_content, panel_footer, panel_configA, panel_configB,title;
	private JTextField time_hrs, time_min;
	private JTextField subject;
	private JCalendarCombo startTime, endTime;
	private JComboBox starthour, endhour,remindertime, snooze_time;
	private Appointment appointment;
	private String dir = "images/";
	private Task task;
	private Alarm alarm;
	private int min=0, hrs=9;
	private String fileselect="";
	private JButton choose, check1, check2;
	private int type;
	private boolean boolReminder, boolSound;
	private JTextField description;
	private boolean b_sound, b_reminder;
	private int timer[][] = new int[48][2];
	private String times[] = new String[6] ;
	private String remindertimes[] = new String[22];
	private GenericEvent evento;
	private ContentView cont;
	private JFileChooser chooser;
	private RecurrenceFrame rf;
	
	
	public AddFrame(int type,GenericEvent event,ContentView cont)
	{
		this.type = type;
		evento = event;
		this.cont= cont;
		
		times[0]="5 Minutes";
		times[1]="10 Minutes";
		times[2]="15 Minutes";
		times[3]="30 Minutes";
		times[4]="1 Hour";
		times[5]="2 Hours";
		
		remindertimes[0]="0 minutes";
		remindertimes[1]="5 minutes";
		remindertimes[2]="10 minutes";
		remindertimes[3]="15 minutes";
		remindertimes[4]="30 minutes";
		int cont2 = 5;
		for(int i=1; i<12;i++)
		{
			remindertimes[cont2]=i+" hours";
			cont2++;
		}
		for(int j=1; j<5;j++)
		{
			remindertimes[cont2]=j+" days";
			cont2++;
		}
		remindertimes[20]="1 week";
		remindertimes[21]="2 weeks";
		
		if(type==0)
		{
			txt+="Appointment";
			appointment = (Appointment)evento;
		}
		else if(type==2)
		{
			txt+="Task";
			task = (Task)evento;
		}
		else
		{
			txt+="Alarm";
			alarm = (Alarm)evento;
		}
		
		createGenericFrame();
		createConfig(type);
		createContent(type);
		
	}
	
	
	
	public void createGenericFrame()
	{
		frame = new JFrame(txt);		
		frame.setLocation(450,350);
		frame.setSize(900,500);

		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		
		JLabel panel = new JLabel();
		panel.setPreferredSize(new Dimension(900,500));
		panel.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		if(type==0)
			panel.setIcon(new ImageIcon(dir+"fondoframe2.png"));
		else if(type==1)
			panel.setIcon(new ImageIcon(dir+"fondoframe2b.png"));
		else
			panel.setIcon(new ImageIcon(dir+"fondoframe2c.png"));
		
		panel_header = new JLabel();
		panel_header.setPreferredSize(new Dimension(880,70));
		panel_header.setLayout(new FlowLayout(FlowLayout.LEFT,20,10));
		
		JLabel txt_sub = new JLabel("Subject: ");
		txt_sub.setPreferredSize(new Dimension(70,50));
		txt_sub.setFont(new Font("Corbel", Font.PLAIN, 18));
		
		subject = new JTextField(evento.getSubject());
		subject.setPreferredSize(new Dimension(600,30));
		
		JLabel empty = new JLabel();
		empty.setPreferredSize(new Dimension(30,30));
		
		JButton recurrence = new JButton();
		recurrence.setIcon(new ImageIcon(dir+"recurrencebt.png"));
		recurrence.setContentAreaFilled(false);
		recurrence.addActionListener(this);
		recurrence.setActionCommand("RECURRENCE");
		recurrence.setPreferredSize(new Dimension(60,40));
		
		panel_header.add(txt_sub);
		panel_header.add(subject);
		panel_header.add(empty);
		panel_header.add(recurrence);		
		
		panel_config = new JLabel();
		panel_config.setPreferredSize(new Dimension(880,150));
		panel_config.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		panel_configA = new JLabel();
		panel_configA.setPreferredSize(new Dimension(500,130));
		panel_configA.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));

		
		panel_configB = new JLabel();
		panel_configB.setPreferredSize(new Dimension(350,130));
		panel_configB.setLayout(new FlowLayout(FlowLayout.LEFT,20,25));

		
		panel_config.add(panel_configA);
		panel_config.add(panel_configB);
		
		
		panel_content = new JLabel();
		panel_content.setPreferredSize(new Dimension(880,160));
		panel_content.setLayout(new FlowLayout(FlowLayout.LEFT));	
		
		
		panel_footer = new JLabel();
		panel_footer.setPreferredSize(new Dimension(880,80));
		panel_footer.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));	
		
		JButton save = new JButton();
		save.setIcon(new ImageIcon(dir+"saveframeint.png"));
		save.setContentAreaFilled(false);
		save.setPreferredSize(new Dimension(150,60));
		save.addActionListener(this);
		save.setActionCommand("SAVE");		
		
		JButton discard = new JButton();
		discard.setIcon(new ImageIcon(dir+"cancelframeint.png"));
		discard.setContentAreaFilled(false);
		discard.setPreferredSize(new Dimension(150,60));
		discard.addActionListener(this);
		discard.setActionCommand("DISCARD");
		
		panel_footer.add(discard);		
		panel_footer.add(save);
		
		
		panel.add(panel_header);
		panel.add(panel_config);
		panel.add(panel_content);
		panel.add(panel_footer);
		frame.add(panel);
		
		frame.setVisible(true);
	}
	
	
	public void createConfig(int t)
	{
		if(t==0 || t==2)
		{
			Appointment a= new Appointment();
			Task tk = new Task();
			if(t==0)
				a= (Appointment)evento;
			else
				tk=(Task)evento;
			
			
			JLabel lab1 = new JLabel();
			if(t==0)
				lab1.setText("Start Time: ");
			else
				lab1.setText("Start Date: ");
			lab1.setPreferredSize(new Dimension(120,30));
			lab1.setFont(new Font("Corbel", Font.PLAIN, 20));			
			
			startTime = new JCalendarCombo();
			Date tmp = new Date();
			if(evento.getD_Start()!=null)
			{
				tmp= evento.getD_Start();
				//tmp.setYear(tmp.getYear()-1900);				
				startTime.setDate(tmp);
			}
		
			startTime.setBackground(Color.white);
			startTime.setFont(new Font("Corbel", Font.BOLD, 12));
			
			starthour = new JComboBox();			
			starthour.setPreferredSize(new Dimension(100,30));
			starthour.setFont(new Font("Corbel", Font.PLAIN, 20));
			starthour.setBackground(Color.white);
			
			
			endTime = new JCalendarCombo();
			endTime.setBackground(Color.white);
			endTime.setFont(new Font("Corbel", Font.BOLD, 12));
			
			endhour = new JComboBox();
			endhour.setPreferredSize(new Dimension(100,30));
			endhour.setFont(new Font("Corbel", Font.PLAIN, 20));
			endhour.setBackground(Color.white);
			
			JLabel lab2 = new JLabel();
			if(t==0)
				lab2.setText("End Time: ");
			else
				lab2.setText("Due Date: ");
			lab2.setPreferredSize(new Dimension(120,30));
			lab2.setFont(new Font("Corbel", Font.PLAIN, 20));
			int cont=0;
			
			for(int i=0; i <24;i++)
			{
				starthour.addItem(i+":00");
				starthour.addItem(i+":30");
				
				endhour.addItem(i+":00");
				endhour.addItem(i+":30");
				
				timer[cont][0]=i;
				timer[cont][1]=0;
				cont++;
				timer[cont][0]=i;
				timer[cont][1]=30;
				
				
			}
		
			if(evento.getD_Start()!=null)
			{
				starthour.setSelectedItem(evento.getT_Start().getHours()+":"+evento.getT_Start().getMinutes());
				if(evento.getType()==0)
				{
					
					//rtemp... para qu eno mande nulls				
					a.setT_Final(tmp);		
					
					
					endhour.setSelectedItem(a.getT_Final().getHours()+":"+a.getT_Final().getMinutes());
					
				}
			}
			
			JLabel vac = new JLabel();
			vac.setPreferredSize(new Dimension(100,30));
						
			panel_configA.add(lab1);
			panel_configA.add(startTime);
			if(t==0)
				panel_configA.add(starthour);
			else 
				panel_configA.add(vac);
			panel_configA.add(lab2);
			panel_configA.add(endTime);
			if(t==0)
				panel_configA.add(endhour);
			panel_configA.updateUI();
			
			check1 = new JButton();
			if(t==0)
			{
				if(a.getReminder()!=null)
				{
					boolReminder=true;
					check1.setIcon(new ImageIcon(dir+"select.png"));
				}
				else
				{
					check1.setIcon(new ImageIcon(dir+"noselect.png"));
					boolReminder= false;
					
				}
				
			}
			
			check1.setPreferredSize(new Dimension(20,20));
			check1.addActionListener(this);
			check1.setActionCommand("REMINDER_CH");
			
			
			
			JLabel reminder = new JLabel("Reminder ");
			reminder.setPreferredSize(new Dimension(100,30));
			reminder.setFont(new Font("Corbel", Font.PLAIN, 20));
			
			remindertime = new JComboBox();
			remindertime.setPreferredSize(new Dimension(150,30));
			remindertime.setFont(new Font("Corbel", Font.PLAIN, 20));
			remindertime.setBackground(Color.white);
			for(int in=0;in<remindertimes.length;in++)
				remindertime.addItem(remindertimes[in]);
			
			if(!boolReminder)
				remindertime.setEnabled(false);
			else
				//sacar tiempo del reminder
				remindertime.setSelectedIndex(1);
			
			check2 = new JButton();
			
			//falta ver donde esta el sonido
			
			check2.setPreferredSize(new Dimension(20,20));
			check2.addActionListener(this);
			check2.setActionCommand("SOUND_CH");
			
			
			JLabel reminder2 = new JLabel("Sound ");
			reminder2.setPreferredSize(new Dimension(100,30));
			reminder2.setFont(new Font("Corbel", Font.PLAIN, 20));
			
			choose = new JButton();
			choose.setIcon(new ImageIcon(dir+"choose.png"));
			choose.setPreferredSize(new Dimension(120,30));
			choose.setFont(new Font("Corbel", Font.PLAIN, 15));
			choose.addActionListener(this);
			choose.setActionCommand("CHOOSE");
			
			panel_configB.add(check1);
			panel_configB.add(reminder);
			panel_configB.add(remindertime);
			panel_configB.add(check2);
			panel_configB.add(reminder2);
			panel_configB.add(choose);
			
		}
		else 
		{
			JLabel lb = new JLabel("Time :");
			lb.setPreferredSize(new Dimension(100,30));
			lb.setFont(new Font("Corbel", Font.PLAIN, 30));
			
			JLabel lb2 = new JLabel();
			lb2.setPreferredSize(new Dimension(250,120));
			lb2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));			
			
			JButton up = new JButton();
			up.setIcon(new ImageIcon(dir+"mas.png"));
			up.setContentAreaFilled(false);
			up.setPreferredSize(new Dimension(90,30));
			up.addActionListener(this);
			up.setActionCommand("UP_HRS");
			
			JLabel empty = new JLabel();
			empty.setPreferredSize(new Dimension(40,30));
			
			JButton up2 = new JButton();
			up2.setIcon(new ImageIcon(dir+"mas.png"));
			up2.setContentAreaFilled(false);
			up2.setPreferredSize(new Dimension(90,30));
			up2.addActionListener(this);
			up2.setActionCommand("UP_MIN");
			
			time_hrs = new JTextField();
			if(hrs<10)
				time_hrs.setText("0"+hrs);
			else
				time_hrs.setText(""+hrs);
			time_hrs.setFont(new Font("Corbel", Font.PLAIN, 35));
			time_hrs.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			time_hrs.setPreferredSize(new Dimension(90,40));
		
			
			JLabel empty2 = new JLabel();
			empty2.setPreferredSize(new Dimension(40,30));
			
			time_min = new JTextField();
			if(min<10)
				time_min.setText("0"+min);
			else
				time_min.setText(""+min);
			time_min.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			time_min.setFont(new Font("Corbel", Font.PLAIN, 35));
			time_min.setPreferredSize(new Dimension(90,40));
		
			
			JButton upd = new JButton();
			upd.setIcon(new ImageIcon(dir+"menos.png"));
			upd.setContentAreaFilled(false);
			upd.setPreferredSize(new Dimension(90,30));
			upd.addActionListener(this);
			upd.setActionCommand("DOWN_HRS");
			
			JLabel emptyd = new JLabel();
			emptyd.setPreferredSize(new Dimension(40,30));			
			
			JButton up2d = new JButton();
			up2d.setContentAreaFilled(false);
			up2d.setIcon(new ImageIcon(dir+"menos.png"));
			up2d.setPreferredSize(new Dimension(90,30));
			up2d.addActionListener(this);
			up2d.setActionCommand("DOWN_MIN");
			
			lb2.add(up);
			lb2.add(empty);
			lb2.add(up2);
			lb2.add(time_hrs);
			lb2.add(empty2);
			lb2.add(time_min);
			lb2.add(upd);
			lb2.add(emptyd);
			lb2.add(up2d);
			
			
			panel_configA.add(lb);
			panel_configA.add(lb2);
			panel_configA.updateUI();
			
			JLabel lab1 = new JLabel("Date : ");
			lab1.setPreferredSize(new Dimension(100,70));
			lab1.setFont(new Font("Corbel", Font.PLAIN, 30));			
			
			startTime = new JCalendarCombo();
			startTime.setBackground(Color.white);
			startTime.setFont(new Font("Corbel", Font.BOLD, 12));
			
			panel_configB.add(lab1);
			panel_configB.add(startTime);
			
			
		}
		
	}
	
	public void createContent(int t)
	{
		if(t==0 || t==2)
		{
			JLabel lb = new JLabel("Description: ");
			lb.setPreferredSize(new Dimension(120,30));
			lb.setFont(new Font("Corbel", Font.PLAIN, 20));
			
			description = new JTextField(evento.getDescription()); 
			description.setPreferredSize(new Dimension(800,100));
			description.setFont(new Font("Corbel", Font.PLAIN, 20));
			
			panel_content.add(lb);
			panel_content.add(description);			
			panel_content.updateUI();
			
		}
		else 
		{
			JLabel emp =  new JLabel();
			emp.setPreferredSize(new Dimension(800,40));
			
			JLabel lb = new JLabel("   Snooze Time :");
			lb.setPreferredSize(new Dimension(150,30));
			lb.setFont(new Font("Corbel", Font.PLAIN, 22));
			
			Alarm a = (Alarm)evento;
			snooze_time = new JComboBox();			
			snooze_time.setPreferredSize(new Dimension(150,30));
			snooze_time.setBackground(Color.white);
			snooze_time.setFont(new Font("Corbel", Font.BOLD, 15));
			
			for(int i=0; i<times.length;i++)
			{
				snooze_time.addItem(times[i]);
			}
			
			if(a.getParams()!=null)
				snooze_time.setSelectedIndex((Integer)a.getParams().get("Snooze"));			
			
			JLabel reminder2 = new JLabel("Ringtone :");
			reminder2.setPreferredSize(new Dimension(100,30));
			reminder2.setFont(new Font("Corbel", Font.PLAIN, 20));
			
			title = new JLabel();
			
			if(a.getParams()!=null)
				title.setText(""+a.getParams().get("Ringtone"));
			title.setPreferredSize(new Dimension(170,30));			
			
			JLabel emp2 = new JLabel();
			emp2.setPreferredSize(new Dimension(120,30));
						
			JButton choose2 = new JButton();
			choose2.setIcon(new ImageIcon(dir+"choose.png"));
			choose2.setPreferredSize(new Dimension(120,30));
			choose2.setFont(new Font("Corbel", Font.PLAIN, 15));
			choose2.addActionListener(this);
			choose2.setActionCommand("CHOOSE");					
			
			panel_content.add(emp);
			panel_content.add(lb);
			panel_content.add(snooze_time);
			panel_content.add(emp2);
			panel_content.add(reminder2);
			panel_content.add(title);
			panel_content.add(choose2);
		}
	}
	

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		System.out.println(arg0.getActionCommand());
		
		if(arg0.getActionCommand().equals("REMINDER_CH"))
		{
			boolReminder = !boolReminder;
			if(boolReminder)
			{
				check1.setIcon(new ImageIcon(dir+"select.png"));
				remindertime.setEnabled(true);
				if(type==0)
				{
					Appointment a = (Appointment)evento;
					Reminder r = new Reminder();
					a.setReminder(r);
				}
			}
			else
			{
				remindertime.setEnabled(false);
				check1.setIcon(new ImageIcon(dir+"noselect.png"));
			}
			check1.updateUI();
			
		}
		
		if(arg0.getActionCommand().equals("UP_MIN"))
		{
			min = Integer.parseInt(time_min.getText());
			min++;
			if(min>=60 )
				min=0;
			
			if(min<10)
				time_min.setText("0"+min);
			else
				time_min.setText(""+min);
			
			time_min.updateUI();
			
		}
		
		else if(arg0.getActionCommand().equals("DOWN_MIN"))
		{
			min = Integer.parseInt(time_min.getText());
			min--;
			if(min<0)
				min=59;
			
			if(min<10)
				time_min.setText("0"+min);
			else
				time_min.setText(""+min);
			
			time_min.updateUI();
			
		}
		
		else if(arg0.getActionCommand().equals("UP_HRS"))
		{
			hrs = Integer.parseInt(time_hrs.getText());
			hrs++;
			if(hrs>=24)
				hrs=0;
			
			if(hrs<10)
				time_hrs.setText("0"+hrs);
			else
				time_hrs.setText(""+hrs);
			
			time_hrs.updateUI();
			
		}
		
		else if(arg0.getActionCommand().equals("DOWN_HRS"))
		{
			hrs = Integer.parseInt(time_hrs.getText());
			hrs--;
			if(hrs<0)
				hrs=23;
			
			if(hrs<10)
				time_hrs.setText("0"+hrs);
			else
				time_hrs.setText(""+hrs);
			
			time_hrs.updateUI();
			
		}
		
		else if(arg0.getActionCommand().equals("SAVE"))
		{
			if (type==0)
			{
				Date date1 = new Date();
				date1.setHours(timer[starthour.getSelectedIndex()][0]);
				date1.setMinutes(timer[starthour.getSelectedIndex()][1]);
				
				Date date2 = new Date();
				date2.setHours(timer[endhour.getSelectedIndex()][0]);
				date2.setMinutes(timer[endhour.getSelectedIndex()][1]);				
				
				appointment.setDescription(description.getText());
				appointment.setT_Final(date2);
				appointment.setT_Start(date1);
				appointment.getParams().put("Ringtone", fileselect);
				appointment.setD_Start(startTime.getDate());
				appointment.setSubject(subject.getText());
				appointment.setD_Final(endTime.getDate());
			}
			else if(type==1)
			{
				alarm.setD_Start(startTime.getDate());				
				Date hour = new Date();
				hour.setHours(Integer.parseInt(time_hrs.getText()));
				hour.setMinutes(Integer.parseInt(time_min.getText()));
				alarm.setT_Start(hour); 
				alarm.getParams().put("Ringtone", fileselect);
				alarm.setSubject(subject.getText());
				alarm.setDescription(description.getText());
			}
			else
			{
				task.setDescription(description.getText());
				task.getParams().put("Ringtone", fileselect);
				task.setSubject(subject.getText());
				task.setD_Start(startTime.getDate());
				task.setSubject(subject.getText());
				task.setD_Final(endTime.getDate());
			}	
			
			cont.actionPerformed(arg0);
			frame.dispose();
		}
		
		else if (arg0.getActionCommand().equals("DISCARD"))
		{
			cont.actionPerformed(arg0);
			frame.dispose();
		}
		
		else if (arg0.getActionCommand().equals("RECURRENCE"))
		{
			rf= new RecurrenceFrame(evento);
		}
		else if(arg0.getActionCommand().equals("CHOOSE"))
		{
			JFileChooser j = new JFileChooser();
			j.setMultiSelectionEnabled(false);
			int rtn = j.showOpenDialog(frame);
			if (rtn == JFileChooser.APPROVE_OPTION)
				fileselect=j.getSelectedFile().getPath();
			
			if(type==1)
			{
				title.setText(fileselect);
				title.updateUI();
			}
			
		}				
		
	}

}